<template>
  <div class="search-header">
    <div class="search-box">
      <el-input
        class="handle-input"
        placeholder="输入专业名称"
        v-model="searchValue"
        @keyup.enter="handleSearch"
      ></el-input>
      <el-button type="primary" @click="handleSearch">
        <el-icon>
          <Search />
        </el-icon>搜索
      </el-button>
    </div>
  </div>
  <el-radio-group
    v-model="degree"
    size="large"
    @change="getSortedMajors"
    style="flex-wrap:nowrap;  width: 100%;  overflow-x: auto;  scrollbar-width: thin; scrollbar-color: #ffffff00 #ffffff00 ;"
  >
    <el-radio-button v-for="item in degreeList" :key="item" :label="item" :value="item" border />
  </el-radio-group>
  <div class="content">
    <div class="major-list">
      <ul>
        <li v-for="o in majorList" :key="o.majors.id">
          <el-card class="majorCard" shadow="hover">
            <div class="major_list_title">{{ o.majors.name }}</div>
            <div class="major_list_tags">
              <span>专业代码：{{ o.majors.code }}</span>&nbsp;|&nbsp;
              <span>类别：{{ o.majorType.mtName }}</span>&nbsp;|&nbsp;
              <span>修业年限：{{ o.majors.years }}</span>&nbsp;|&nbsp;
              <span>学位：{{ o.degrees.dName }}</span>
            </div>
          </el-card>
        </li>
      </ul>
    </div>
    <div class="heat-rank">
      <el-card class="rankCard" shadow="hover">
        <template #header>
          <h2 style="text-align: center; margin: 0 auto">专业热度</h2>
        </template>
        <ul>
          <li v-for="(item, index) in majorRankList" :key="index" class="heat-item">
            <span
              v-if="index == 0"
              class="badge"
              style="color: #fff; background: #da1d1d"
            >{{ index + 1 }}</span>
            <span
              v-if="index == 1"
              class="badge"
              style="color: #fff; background: #fc6639"
            >{{ index + 1 }}</span>
            <span
              v-if="index == 2"
              class="badge"
              style="color: #fff; background: #f6b759"
            >{{ index + 1 }}</span>
            <span
              v-if="(index !== 0) & (index !== 1) & (index !== 2)"
              class="badge"
              style="color: #999; background: #eee"
            >{{ index + 1 }}</span>
            <span class="major-name">{{ item.major }}</span>
            <span class="heat-value">{{ item.heat }}</span>
          </li>
        </ul>
      </el-card>
    </div>
  </div>
  <el-pagination
    class="pagination"
    background
    v-model:current-page="pageNum"
    :total="total"
    layout="prev, pager, next, jumper"
    @current-change="handleCurrentChange"
  />
</template>

<script setup>
import { ref, watch, onMounted } from 'vue'
import { ElMessage, ElLoading } from 'element-plus'
import request from '@/utils/request.js'

import majorRankList from '@/assets/majorRankList.json'
const degreeList = ref(['全部'])
const searchValue = ref('')

const degree = ref('全部')
const pageNum = ref(1)
const total = ref(0)
const majorList = ref([])

watch(degree, () => {
  pageNum.value = 1
})

const handleSearch = async () => {
  const loadingInstance = ElLoading.service({
    fullscreen: true,
    text: '正在加载中...',
  })
  degree.value = '全部'
  try {
    const response = await request.get(
      '/major/searchByName?page=' +
        pageNum.value +
        '&majorName=' +
        searchValue.value
    )
    if (response.code == 200) {
      total.value = response.data.total
      majorList.value = response.data.majors
    } else {
      ElMessage.error(response.message)
    }
  } catch (error) {
    ElMessage.error(error)
  }
  loadingInstance.close()
}

const getSortedMajors = async () => {
  const loadingInstance = ElLoading.service({
    fullscreen: true,
    text: '正在加载中...',
  })
  searchValue.value = ''
  try {
    const response = await request.get(
      '/major/list?page=' + pageNum.value + '&degreesName=' + degree.value
    )
    if (response.code == 200) {
      total.value = response.data.total
      majorList.value = response.data.data
    } else {
      ElMessage.error(response.message)
    }
  } catch (error) {
    ElMessage.error(error)
  }
  loadingInstance.close()
}

const handleCurrentChange = (newPage) => {
  pageNum.value = newPage
  if (searchValue.value) handleSearch()
  else getSortedMajors()
}
const getDergrees = async () => {
  try {
    const res = await request.get('/degrees/list')
    console.log(res.code)
    if (res.code == 200) {
      var list = res.data

      list.forEach((element) => {
        console.log(element)
        degreeList.value.push(element.dName)
      })
      // for (degree in list) {
      //   console.log(degree)
      //   degreeList.value.push(degree.name)
      // }
    } else {
      ElMessage.error(res.message)
    }
  } catch (error) {
    ElMessage.error(error)
  }
}
onMounted(getDergrees)
onMounted(getSortedMajors)
</script>

<style scoped>
.search-header {
  display: flex;
  justify-content: end;
  margin: 2vh auto;
  height: 40px;
}

.search-box {
  display: flex;
  width: 50%;
}

.el-radio {
  margin: 2px 5px;
}
.handle-input {
  width: 100%;
}

.handle-input :deep(.el-input__wrapper) {
  border-radius: 4px 0 0 4px;
}

.el-button {
  height: 40px;
  border-radius: 0 4px 4px 0;
}

.content {
  display: flex;
}

.major-list {
  flex: 7;
}

.heat-rank {
  flex: 3;
  margin-top: 20px;
  margin-left: 10px;
}

ul {
  justify-content: center;
  padding-inline-start: 0;
}

li {
  list-style-type: none;
}

.majorCard {
  padding: 0;
  margin: 10px auto;
  height: 80px;
  border-radius: 10px;
  text-align: center;
}

.majorCard:hover {
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.2);
  background-color: rgb(248, 236, 207);
  cursor: pointer;
}

.majorCard :deep(.el-card__body) {
  padding: 0;
}

.major_list_title {
  font-size: 20px;
  font-weight: bold;
  margin: 10px auto;
  transition: all 0.4s;
}

.major_list_tags {
  display: flex;
  justify-content: space-around;
  color: var(--theme--color);
  margin-top: 5px;
}
.major_list_tags span {
  color: black;
  min-width: 20%;
}
.rankCard {
  border-radius: 10px;
}

.rankCard :deep(.el-card__body) {
  padding: 0 20px;
}

.heat-item {
  display: flex;
  align-items: center;
  margin: 20px 0;
  cursor: pointer;
}

.major-name {
  font-size: 16px;
  font-weight: bold;
  margin-left: 4px;
}

.heat-value {
  font-size: 18px;
  color: rgb(237, 33, 33);
  margin-left: auto;
}

.badge {
  width: 20px;
  height: 20px;
  font-size: 15px;
  text-align: center;
  line-height: 20px;
  overflow: hidden;
  font-weight: normal;
  margin-right: 3px;
}

.pagination {
  justify-content: center;
}
</style>
